<!DOCTYPE html>
<html class="x-admin-sm">
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.2</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <link rel="stylesheet" href="/css/font.css">
    <link rel="stylesheet" href="/css/xadmin.css">
    <script src="/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="/js/xadmin.js"></script>
    <script type="text/javascript" src="/js/jquery.min.js"></script>
</head>
<style type="text/css">
    .layui-table-cell {
        height: auto !important;
        white-space: normal;
    }
</style>
<body>
<div class="x-nav">
            <span class="layui-breadcrumb">
                <a>首页</a>
                <a>商品管理</a>
                <a>
                    <cite>商品列表</cite>
                </a>
            </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
       onclick="location.reload()" title="刷新">
        <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
    </a>
</div>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body ">
                    <input type="hidden" id="mid1" th:value="${mid}">
                    <form class="layui-form layui-col-space5" >
                        <div class="layui-input-inline layui-show-xs-block">
                            <input type="text" name="name" placeholder="请输入名称" autocomplete="off"
                                   class="layui-input">
                        </div>
                        <div class="layui-input-inline layui-show-xs-block">
                            <input type="text" name="price" placeholder="请输入价格" autocomplete="off"
                                   class="layui-input">
                        </div>
                        <div class="layui-input-inline layui-show-xs-block">
                            <input type="text" name="content" placeholder="请输入描述" autocomplete="off"
                                   class="layui-input">
                        </div>
                        <div class="layui-input-inline layui-show-xs-block">
                            <input type="text" name="stock" placeholder="请输入库存" autocomplete="off"
                                   class="layui-input">
                        </div>
                        <div class="layui-inline layui-show-xs-block">
                            <select class="layui-select"  name="isSell">
                                <option  value="0">销售</option>
                                <option  value="1">禁售</option>
                            </select>
                        </div>
                        <div class="layui-input-inline layui-show-xs-block">
                            <button class="layui-btn" lay-submit="" lay-filter="search">
                                <i class="layui-icon">&#xe615;</i></button>
                        </div>
                    </form>
                </div>
                <div class="layui-card-header">
                    <button class="layui-btn" id="add" onclick="add()" lay-event="add" >
                        <i class="layui-icon"></i>添加商品
                    </button>
                </div>
                <div class="layui-card-body ">
                    <table class="layui-hide" id="test" lay-filter="demo"></table>
                </div>
                <input type="hidden" id="mid" th:value="${#httpSession.getAttribute('mid')}">
            </div>
        </div>
    </div>
</div>
</body>
<script type="text/html" id="currentTableBar">
    <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
</script>
<script>
    var mid = $("#mid").val();
    var mid1 =$("#mid1").val();
    if (mid1 !== null && mid1 !== ''){
       var mid = $("#mid1").val();
    }else {
      var mid = $("#mid").val();
    }

    layui.use([ 'form', 'table'],
        function () {
            var
                table = layui.table,
                form = layui.form;


            //执行一个 table 实例
            // 监听搜索操作
            form.on('submit(search)', function (data) {
                var data = data.field
                console.log(data)
                //执行搜索重载
                table.reload('test', {
                    page: false
                    , where: {
                        name: data.name,
                        isSell: data.isSell,
                        content: data.content,
                        price: data.price,
                        stock: data.stock,
                        mid: mid
                    }
                    ,url: '/product/findByProduct'
                    , parseData: function (res) { //res 即为原始返回的数据
                        console.log(res)
                        return {
                            "code": 0, //解析接口状态
                            "msg": res.msg, //解析提示文本
                            "data": res.data, //解析数据列表
                        }
                    }
                }, 'data');
                return false;
            });

            table.render({
                elem: '#test'
                , url: '/product/findAllProductByMid?mid=' + mid //数据接口
                , page: true //开启分页
                , height: 450
                , cols: [
                    [ //表头
                        {field: 'id', title: '商品id', width: 90, align: 'center'},
                        {field: 'name', title: '商品名称', width: 90, align: 'center'}
                        , {
                        field: 'image', title: '商品图片', width: 150, align: 'center', templet: function (res) {
                            if (res.image==""){
                                return '<img style="width: 150px;height: 100px" src="/images/tp.jpg">'
                            }
                            return '<img style="width: 150px;height: 100px" src=' + res.image + '>'
                        }
                    }
                        , {field: 'content', title: '商品介绍', width: 200}
                        , {field: 'price', title: '商品价格', sort: true, width: 100, align: 'center'}
                        , {field: 'stock', title: '商品库存', width: 90, align: 'center'}
                        , {
                        field: 'isSell', title: '商品状态', width: 90, align: 'center', templet: function (res) {
                            if (res.isSell == 1) {
                                return '<a lay-event="setIsSell"><div style="color: red ;font-weight:bold">禁售</div></a>'
                            } else {
                                return '<a lay-event="setIsSell"><div style="color: green">销售</div></a>'
                            }
                        }
                    }
                    , {field: 'sold', title: '商品销量', sort: true, width: 100, align: 'center'}
                    , {field: 'createTime', title: '创建时间', sort: true, width: 150, align: 'center'}
                    , {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
                    ]
                ]
                , parseData: function (res) { //res 即为原始返回的数据
                    return {
                        "code": res.code, //解析接口状态
                        "msg": res.msg, //解析提示文本
                        "count": res.data.total, //解析数据长度
                        "data": res.data.records //解析数据列表
                    }
                }
                ,done: function(res,curr,count){
                    hoverOpenImg();//显示大图
                    $('table tr').on('click',function(){
                        $('table tr').css('background','');
                        $(this).css('background','<%=PropKit.use("config.properties").get("table_color")%>');
                    });
                }
            });

            table.on('tool(demo)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
                const data = obj.data; //获得当前行数据
                const layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
                if (layEvent === 'setIsSell') {
                    //在此设置用户状态
                    const id = data.id;
                    const isSell = data.isSell;
                    if (isSell === 0) {
                        $.ajax({
                            url: 'product/updateProductIsSell?id=' + id,
                            type: 'put',
                            dataType: 'json'
                        })
                        location.reload()
                    } else {
                        $.ajax({
                            url: 'product/updateProductIsSell?id=' + id,
                            type: 'put',
                            dataType: 'json'
                        })
                        location.reload()
                    }

                } else if (layEvent == 'edit') {
                    var index = layer.open({
                        title: '编辑商品',
                        type: 2,
                        shade: 0.2,
                        maxmin: false,
                        shadeClose: true,
                        area: ['40%', '80%'],
                        content: 'product/toProductEdit?id=' + data.id + '&mid='+data.mid
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                } else if (layEvent == 'delete') {
                    let id = data.id
                    console.log(id)
                    layer.confirm('确认要删除吗？', function () {
                        $.ajax({
                            url: '/product/deleteProduct?pid=' + id,
                            type: 'delete',
                            dataType: 'json',
                            async: false,
                            success: function (res) {
                                layer.msg(res.msg, {icon: 1, time: 1000}, function () {
                                    // 可以对父窗口进行刷新
                                    xadmin.father_reload();
                                });
                            }
                        })

                    });
                }
            })


        });

    function add() {
        xadmin.open('添加商品','product/toProductEdit?mid='+mid,500,600)
    }

    function hoverOpenImg(){
        var img_show = null; // tips提示
        $('td img').hover(function(){
            //alert($(this).attr('src'));
            var img = "<img class='img_msg' src='"+$(this).attr('src')+"' style='width:130px;' />";
            img_show = layer.tips(img, this,{
                tips:[2, 'rgba(41,41,41,.5)']
                ,area: ['160px']
            });
        },function(){
            layer.close(img_show);
        });
        $('td img').attr('style','max-width:70px');
    }
</script>

</html>